package handler

import (
	"exam/api/request"
	"exam/internal/dao"
	"exam/internal/model"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func AddExam(c *gin.Context) {
	var req request.AddExam
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Exam
	dao.GetOneByFields(&model.Exam{Title: req.Title}, &data)
	if data.Id > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该场考试已被创建",
			"data":    nil,
		})
		return
	}

	exam := model.Exam{
		Title:     req.Title,
		StartTime: req.StartTime,
		LongTime:  req.LongTime,
		CType:     req.CType,
	}
	if !dao.Create(&exam) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "新考试创建失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "新考试创建成功",
		"data":    exam,
	})
}

func QueryExamById(c *gin.Context) {
	var req request.QueryExamById
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Exam
	dao.GetOneById(req.Id, &data)
	if data.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该场考试不存在",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "考试详情展示成功",
		"data":    data,
	})

}

func UpdateExam(c *gin.Context) {
	var req request.UpdateExam
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Exam
	dao.GetOneById(req.Id, &data)
	if data.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该场考试不存在",
			"data":    nil,
		})
		return
	}

	update := dao.Update(&model.Exam{
		Id:        data.Id,
		Title:     req.Title,
		StartTime: req.StartTime,
		LongTime:  req.LongTime,
		CType:     req.CType,
	})

	if !update {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "考试信息修改失败",
			"data":    nil,
		})
		return
	}

	var exam model.Exam
	dao.GetOneById(req.Id, &exam)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "考试信息修改成功",
		"data":    exam,
	})

}

func DeleteExam(c *gin.Context) {
	var req request.UpdateExam
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Exam
	dao.GetOneById(req.Id, &data)
	if data.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该场考试不存在",
			"data":    nil,
		})
		return
	}
	if !dao.Delete(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该场考试移除失败",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "考试移除成功",
		"data":    nil,
	})

}

func QueryExamByCType(c *gin.Context) {
	var req request.QueryExamByCType
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var data []model.Exam

	log.Println(req.CType)
	if !dao.GetListByFields(&model.Exam{CType: req.CType}, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "考试分类查询失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "考试分类查询成功",
		"data":    data,
	})
}
